package com.chrisevansdev.irisysaudit;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.widget.GridView;
import android.widget.TextView;

public class Audit extends Activity 
{
	//------------------------------------------------------
		
	private Handler mHandler = new Handler();
	private Long mStartTime;
	private Long mSystemStartTime;
	private String mStrCurrentTime;
	private String mStoreName;
	private CheckoutManager mCheckoutMgr;
	
	//------------------------------------------------------
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.audit);
		
		// get intent returns the intent that started this activity
		Bundle config = getIntent().getExtras();
		
		// get the storename 
		mStoreName = config.getString("storeName");
		
		// get the checkouts from the config and add to checkout manager
        mCheckoutMgr = new CheckoutManager(this);
		for (String str : config.getStringArrayList("checkouts"))
		{
			mCheckoutMgr.AddCheckout(str);
   		}
		
		GridView gridview = (GridView) findViewById(R.id.checkoutGrid);
		gridview.setAdapter(mCheckoutMgr);
		
		
		// get the start time and store as a long	
		String strStartTime = config.getString("startTime");	
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		mStartTime = 0L;
		try 
		{
			Date d = df.parse(strStartTime);
			mStartTime = d.getTime();
		} 
		catch 
		(ParseException e) {} 	
				
		
		// store system uptime at start and setup time update handler 
		mSystemStartTime = SystemClock.uptimeMillis();
        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 100);
	}
	
	//------------------------------------------------------
	
    @Override
	protected void onPause() {
		// TODO Make this save the checkouts and time
		super.onPause();
	}
    
	//------------------------------------------------------

    @Override
	protected void onResume() {
		// TODO Make this save the checkouts and time
		super.onResume();
	}
	
	//------------------------------------------------------
	
	@Override 
    public void onSaveInstanceState(Bundle outState) 
    {
    	// save the checkout manager state
    	
		ArrayList<String> checkoutNames = new ArrayList<String>();
    	ArrayList<Integer> checkoutCounts = new ArrayList<Integer>();
    	
    	for (CheckoutManager.Checkout c : mCheckoutMgr.getCheckoutArray())
    	{
    		checkoutNames.add(c.getName());
    		checkoutCounts.add(c.getCount());
    	}
    	
    	outState.putStringArrayList("CheckoutNames", checkoutNames);
        outState.putIntegerArrayList("CheckoutCounts", checkoutCounts);
		
		//outState.putSerializable("CheckoutManager", mCheckoutMgr);
        outState.putLong("SystemStartTime", mSystemStartTime);
                
        super.onSaveInstanceState(outState); 
    }

    //------------------------------------------------------
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
        
        ArrayList<String> checkoutNames = savedInstanceState.getStringArrayList("CheckoutNames");
    	ArrayList<Integer> checkoutCounts = savedInstanceState.getIntegerArrayList("CheckoutCounts");
        
    	mCheckoutMgr.getCheckoutArray().clear();
    	for (int i = 0; i < checkoutNames.size(); ++i)
    	{
    		mCheckoutMgr.AddCheckout(checkoutNames.get(i), checkoutCounts.get(i));
    	}
    	    	
        //mCheckoutMgr = (CheckoutManager)savedInstanceState.getSerializable("CheckoutManager");
    	mSystemStartTime = savedInstanceState.getLong("SystemStartTime");
    }
    
	//------------------------------------------------------
	
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() 
		{
		       long elapsed = SystemClock.uptimeMillis() - mSystemStartTime;
		      		    		       		       
		       // update time on UI
		       Time t = new Time(mStartTime+elapsed);
		       mStrCurrentTime = t.toString();
		       
		       TextView mTimeLabel = (TextView)findViewById(R.id.auditTime);
		       mTimeLabel.setText(mStoreName + " - " + mStrCurrentTime);
		         
		       // schedule next update for 1 seconds time 
		       mHandler.postAtTime(this, mSystemStartTime + elapsed + 1000);
		   }
		};
		
	//------------------------------------------------------

}
